<template>
	<view class="bubble-container">
		<uni-icons @click.native.stop="changeShow" class="icon" :type="icon" size="25" color="000"></uni-icons>
		<view v-show="show" :style="bubbleStyle" class="bubble">
			<view class="bubble-item" v-for="(item,index) in dataList" :key="index" @click="onIconItem(index)">
				<image :src="item.iconSrc" class="bubble-icon"></image>
				<text class="bubble-text">{{ item.text }}</text>
				<view class="arrow"></view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		props: {
			icon: {
				type: String,
				required: true
			},
			bubblePosition: {
				type: String,
				default: 'top'
			},
			dataList: {
				type: Array,
				default: () => {
					return []
				}
			},
			showBubble: {
				type: Boolean,
				default: false
			}
		},
		watch: {
			showBubble: {
				immediate: true,
				handler(newValue, oldValue) {
					this.show = newValue
				}
			}
		},

		data() {
			return {
				show: this.showBubble
			}
		},
		computed: {
			bubbleStyle() {
				let style = '';
				if (this.bubblePosition === 'top') {
					style = 'top: -20px; left: 50%; transform: translateX(-50%);';
				} else if (this.bubblePosition === 'bottom') {
					style = 'bottom: -200%; right: -35%; transform: translateX(-50%);transform: translateY(50%);';
				}
				return style;
			}
		},
		methods: {
			changeShow() {
				this.show = !this.show
				this.$emit("change", this.show)
			},
			onIconItem(index) {
				this.$emit("onIconItem", index)
			}
		}
	}
</script>

<style>
	.bubble-container {
		position: relative;
		display: inline-block;
	}

	.icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
	}

	.bubble {
		display: flex;
		flex-direction: column;
		position: absolute;
		background-color: #525252;
		color: #fff;
		/* padding: 10rpx 20rpx; */
		border-radius: 10rpx;
		font-size: 24rpx;
		white-space: nowrap;
		display: flex;
		align-items: center;
		width: 220rpx;
		height: 280rpx;
	}

	.bubble-item {
		display: flex;
		/* justify-content: center; */
		width: 100%;
		align-items: center;
		height: 110rpx;
		line-height: 40rpx;
		border-bottom: 1rpx solid #565656;
	}

	.bubble-item:last-child {
		border: none;
	}

	.bubble-icon {
		margin: 0 20rpx 0 20rpx;
		width: 40rpx;
		height: 40rpx;
	}

	.bubble-text {
		font-size: 30rpx;
		font-weight: 600;
		width: 140rpx;
		text-align: left;
	}

	.arrow {
		position: absolute;
		width: 0;
		height: 0;
		border-left: 10rpx solid transparent;
		border-right: 10rpx solid transparent;
		border-bottom: 10rpx solid #525252;
		top: -10rpx;
		left: 80%;
		transform: translateX(-50%);
	}
</style>